package org.momo.rpc.springboot.starter.config;

import org.momo.rpc.config.RpcConfigLoader;
import org.momo.rpc.config.config.RpcConfig;
import org.momo.rpc.extention.ExtensionLoader;
import org.momo.rpc.registry.ConsumerRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


//@Configuration
public class RpcClientAutoConfig {
    @Bean
    public RpcConfig rpcConfig() {
        RpcConfigLoader.init();
        RpcConfig rpcConfig = RpcConfigLoader.getRpcConfig();
        return rpcConfig;
    }

    @Bean
    public ConsumerRegistry consumerRegistry(RpcConfig rpcConfig) {
        ConsumerRegistry consumerRegistry = ExtensionLoader.getExtensionLoader(ConsumerRegistry.class).getExtension(rpcConfig.getRegistry());
        try {
            consumerRegistry.initRegistryClient(rpcConfig.getRegistryConfig());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return consumerRegistry;
    }
}
